﻿using System;
using System.Windows;
using System.Windows.Media.Imaging;
using AzukiMap.Element;
using GeoAPI.Geometries;
using AzukiMap.Tools;

namespace AzukiMap.Request
{
    public delegate void RequestImageOpenedHandler(object sender, RequestImageOpenedHandlerArgs e);
    public delegate void RequestImageFailedHandler(object sender, RequestImageFailedHandlerArgs e);

    public abstract class ImageRequest
    {
        /*
        public event RequestImageOpenedHandler ImageOpened;
        public event RequestImageFailedHandler ImageFailed;
         */

        protected GeometryUtil _geomUtil;

        public ImageRequest()
        {

        }

        /*
        public virtual void Request(IPolygon imageExtent, int srid)
        {
            BitmapImage image = new BitmapImage(GetUri(imageExtent.EnvelopeInternal));
            GeoImage geoImage = new GeoImage(image, imageExtent, srid);
            geoImage.ImageOpened += new RoutedEventHandler(geoImage_ImageOpened);
            geoImage.ImageFailed += new ExceptionRoutedEventHandler(geoImage_ImageFailed);
        }

        void geoImage_ImageOpened(object sender, RoutedEventArgs e)
        {
            if (ImageOpened != null)
            {
                ImageOpened(this,
                    new RequestImageOpenedHandlerArgs((GeoImage)sender, e.OriginalSource));
            }
        }

        void geoImage_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            if (ImageFailed != null)
            {
                ImageFailed(this,
                    new RequestImageFailedHandlerArgs((GeoImage)sender, e.OriginalSource, e.ErrorException));
            }
        }
         */

        public abstract Uri GetUri(IEnvelope imgExtent);

    }

    public class RequestImageOpenedHandlerArgs : EventArgs
    {
        GeoImage _image;
        object _originalSource;

        public GeoImage Image
        {
            get { return _image; }
        }

        public object OriginalSource
        {
            get { return _originalSource; }
        }

        public RequestImageOpenedHandlerArgs(GeoImage image, object originalSource)
        {
            _image = image;
            _originalSource = originalSource;
        }

    }

    public class RequestImageFailedHandlerArgs : RequestImageOpenedHandlerArgs
    {
        Exception _exception;

        public Exception Exception
        {
            get { return _exception; }
            set { _exception = value; }
        }

        public RequestImageFailedHandlerArgs(GeoImage image, object originalSource, Exception exception)
            : base(image, originalSource)
        {
            _exception = exception;
        }

    }
}
